<template>
  <el-dialog title="新增员工" :visible="isShowDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="addFrom" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <!-- 聘用形式的下拉列表 -->
        <el-select
          v-model="formData.formOfEmployment"
          style="width:80%"
          placeholder="请选择"
        >
          <el-option
            v-for="type in employees.hireType"
            :key="type.id"
            :value="type.id"
            :label="type.value"
          ></el-option>
        </el-select></el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:80%" placeholder="请选择部门" @focus="isShowTree = true" />
        <!-- 部门的树形结构 -->
        <el-tree
          v-if="isShowTree"
          :data="departmentList"
          :props="{label:'name'}"
          default-expand-all
          class="departmentTree"
          @node-click="nodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnSure">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import employees from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
import { listToTreeData } from '@/utils/index'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      // 枚举
      employees,

      // 保存后台返回的数据
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      // 所有部门
      departmentList: [],
      // 部门 树形默认是隐藏的
      isShowTree: false,

      // 对表单进行校验
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
    }
  },

  async created() {
    const { depts } = await getDepartments()
    // 把转换好的树形赋值给 departmentList 默认是平级的
    this.departmentList = listToTreeData(depts, '')
  },

  methods: {
    // 属性节点被点击时的回调
    nodeClick(data) {
      // 赋值 + 关闭树形
      this.formData.departmentName = data.name
      this.isShowTree = false
    },

    // 确定
    async btnSure() {
      // 验证表单 发起请求 通知用户 关闭弹窗 刷新页面
      try {
        await this.$refs.addFrom.validate()
        await addEmployee(this.formData)
        this.$message.success('新增成功')
        this.$parent.isShowDialog = false
        this.$parent.getDepartments()
      } catch (error) {
        console.log(error)
      }
    },

    // 取消
    btnCancel() {
      // 清空表单的内容 重置表单 关闭dialog弹窗
      this.formData = { }
      this.$refs.addFrom.resetFields()
      this.$parent.isShowDialog = false
    }
  }
}
</script>

<style scoped>
.departmentTree {
  width: 80%;
  border: 1px solid #888;
  position: absolute;
  height: 200px;
  z-index: 9;
  overflow: auto;
}
</style>
